package com.applet.mallappletbackstage.controller;


import com.applet.mallappletbackstage.entity.Nation;
import com.applet.mallappletbackstage.entity.WorkType;
import com.applet.mallappletbackstage.entity.vo.WorkTypeVO;
import com.applet.mallappletbackstage.service.NationService;
import com.applet.mallappletbackstage.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X
 * @since 2020-08-13
 */
@RestController
@RequestMapping("/mallappletbackstage/nation")
public class NationController {

    @Autowired
    private NationService nationService;

    @GetMapping("/getAllNation")
    public R getAllNation() {
        List<Nation> list = nationService.list();
        ArrayList<String> nations = new ArrayList<>();
        for (Nation nation : list) {
            String type = nation.getNation();
            nations.add(type);
        }
        return R.success(nations);
    }

    /***
     * 保存民族
     * @param nation
     * @return
     */
    @PostMapping("/saveNation")
    public R saveNation(@RequestParam String nation) {
        int count = nationService.nationService(nation);
        if (count > 0) {
            return R.success("保存成功");
        }
        return R.fail("保存失败");
    }

    /***
     * 通过id获取民族
     * @param id
     * @return
     */
    @GetMapping("/getNation/{id}")
    public R getNation(@PathVariable(value = "id") Integer id) {
        Nation nation = nationService.getNation(id);
        return R.success(nation);
    }

}

